package service.impl;

import common.consts.Consts;
import common.consts.Coupons;
import common.consts.YesNo;
import dao.ActiveCouponDao;
import dao.CouponDao;
import domain.ActiveCoupon;
import domain.Coupon;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.ActiveCouponService;
import service.ServiceResponse;

import java.util.Date;
import java.util.List;

/**
 * Created by aceyin on 15/6/27.
 */
@Service
public class ActiveCouponServiceImpl extends ServiceBase implements ActiveCouponService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ActiveCouponServiceImpl.class);
    @Autowired
    private ActiveCouponDao activeCouponDao;
    @Autowired
    private CouponDao couponDao;

    @Override
    public ServiceResponse getMyActiveCoupons(long owner) {
        List<ActiveCoupon> coupons = activeCouponDao.findValidCoupons(owner);
        if (coupons == null) {
            return ServiceResponse.fail(Consts.i18nKey.NO_INVALID_COUPONS_FOR_USER);
        }
        return ServiceResponse.success().with("coupons", coupons);
    }

    @Override
    @Transactional(readOnly = false)
    public ServiceResponse activeCoupon(long owner, String code) {
        Coupon coupon = couponDao.findByCode(code);
        if (coupon == null) {
            return ServiceResponse.fail(Consts.i18nKey.INVALID_COUPON_CODE);
        }
        if (YesNo.isYes(coupon.getActived())) {
            return ServiceResponse.fail(Consts.i18nKey.COUPON_HAS_ALREADY_ACTIVED);
        }
        if (!Coupons.Status.isValid(coupon.getStatus())) {
            return ServiceResponse.fail(Consts.i18nKey.INVALID_COUPON);
        }
        String type = coupon.getType();
        ActiveCoupon activeCoupon = new ActiveCoupon();
        Date activeDate = new Date();

        if (Coupons.Type.isCash(type)) {
            Integer denomination = coupon.getDenomination();
            if (denomination == null || denomination.intValue() <= 0) {
                return ServiceResponse.fail(Consts.i18nKey.INVALID_COUPON_DENOMINATION);
            }
            activeCoupon.setValidTo(activeDate);
        } else {
            Integer validityDays = coupon.getValidityDays();
            if (validityDays == null || validityDays.intValue() <= 0) {
                return ServiceResponse.fail(Consts.i18nKey.INVALID_COUPON_VALIDITY_DAYS);
            }
            Date validTo = DateUtils.addDays(activeDate, validityDays);
            activeCoupon.setValidTo(validTo);
        }

        couponDao.activeCoupon(code);

        activeCoupon.setAccountId(owner);
        activeCoupon.setActiveDate(activeDate);
        activeCoupon.setCode(coupon.getCode());
        activeCoupon.setType(type);
        activeCouponDao.insert(activeCoupon);

        // 最后返回刚刚激活的代金券
        List<ActiveCoupon> coupons = activeCouponDao.findValidCoupons(owner);
        return ServiceResponse.success().with("coupons", coupons);
    }

    public void setActiveCouponDao(ActiveCouponDao activeCouponDao) {
        this.activeCouponDao = activeCouponDao;
    }

    public void setCouponDao(CouponDao couponDao) {
        this.couponDao = couponDao;
    }
}
